package com.shundr.shipper.common;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.RelativeLayout;
import android.widget.SpinnerAdapter;
import android.widget.TextView;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.OnNavigationListener;
import com.shundr.shipper.R;
import com.shundr.shipper.base.BaseActivity;
import com.shundr.shipper.cargo.CargoDetailsActivity;
import com.shundr.shipper.common.adapter.HistoryAdapter;
import com.shundr.shipper.common.http.CommonRequest;
import com.shundr.shipper.common.model.HistoryInfo;
import com.shundr.shipper.common.util.CommonConfig;
import com.shundr.shipper.common.util.ProgressDialogHelper;
import com.shundr.shipper.common.util.ToastHelper;
import com.shundr.shipper.common.view.CListView;
import com.shundr.shipper.common.view.CListView.OnLoadMoreListener;
import com.shundr.shipper.common.view.CListView.OnRefreshListener;
import com.shundr.shipper.frame.constant.LContext;
import com.shundr.shipper.frame.http.LHttpRequest;
import com.shundr.shipper.truck.MyTruckDetailActivity;
import com.shundr.shipper.truck.TruckCommonDetailActivity;

public class HistoryListActivity extends BaseActivity {

	private static final int PAGE_SIZE = LContext.PAGE_SIZE;
	private int pageNo = 1;
    
	private List<HistoryInfo> mList = new ArrayList<HistoryInfo>();

	private HistoryAdapter mAdapter;
	
	private CListView mListView;
	private RelativeLayout mLayoutNoContent;
	private TextView tvNoContent;
	
	private int mType = CommonConfig.OPERATE_ALL;//默认全部
	
	@SuppressLint("HandlerLeak")
	private Handler mHandler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			try {
				switch (msg.what) {
				case CommonRequest.GET_HISTORY_LIST_SUCCEED:
					@SuppressWarnings("unchecked")
					List<HistoryInfo> infos = (List<HistoryInfo>)msg.obj;
					if (infos != null) {
						if (infos.size() > 0) {
							if (pageNo == 1) {
								mList.clear();
								mListView.setVisibility(View.VISIBLE);
								mLayoutNoContent.setVisibility(View.GONE);
								if (infos.size() >= PAGE_SIZE) {
									mListView.setCanLoadMore(true);
									mListView.setAutoLoadMore(true);
									mListView.setCanRefresh(true);
								} else {
									mListView.setCanLoadMore(false);
									mListView.setAutoLoadMore(false);
									mListView.setCanRefresh(false);
								}
							}
							mList.addAll(infos);
							mAdapter.notifyDataSetChanged();
							if (pageNo == 1) {
								mListView.setSelection(0);
							}
							pageNo = pageNo + 1;
						} else {
							if (pageNo == 1) {
								mListView.setVisibility(View.GONE);
								mLayoutNoContent.setVisibility(View.VISIBLE);
							} else {
								ToastHelper.toastLong(mContext, "最后一页");
							}
						}
					}
					ProgressDialogHelper.dismiss();
					mListView.onRefreshComplete();
					mListView.onLoadMoreComplete();
					break;
				case LHttpRequest.REQUEST_FAILED:
					mListView.onLoadMoreComplete();
					mListView.onRefreshComplete();
					ProgressDialogHelper.dismiss();	
					ToastHelper.toastShort(mContext, msg.obj.toString());
					break;
				default:
					break;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			super.handleMessage(msg);
		}
		
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
		 // 生成一个SpinnerAdapter
		SpinnerAdapter adapter = ArrayAdapter.createFromResource(this,
				R.array.history_status,
				R.layout.item_actionbar_spinner);
       // 为ActionBar设置下拉菜单和监听器
       mActionBar.setListNavigationCallbacks(adapter, new DropDownListenser());
		
		setContentView(R.layout.activity_car_list);
		mListView = (CListView) findViewById(R.id.lv_list);
		mLayoutNoContent = (RelativeLayout) findViewById(R.id.layout_no_content);
		tvNoContent = (TextView) findViewById(R.id.tv_no_content);
		tvNoContent.setText("没有浏览历史");
		mAdapter = new HistoryAdapter(mList,mContext);
		mListView.setAdapter(mAdapter);
		mListView.setCanLoadMore(false);
		mListView.setAutoLoadMore(false);
		mListView.setCanRefresh(true);
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent intent = null;
				HistoryInfo info = mList.get(position - 1);
				switch (info.getObjectType()) {
				case CommonConfig.OPERATE_OBJECT_CARGO:
					intent = new Intent(mContext, CargoDetailsActivity.class);
					intent.putExtra("CargoId", info.getObjectId() + "");
					startActivity(intent);
					break;
				case CommonConfig.OPERATE_OBJECT_TRUCK:
					intent = new Intent(mContext, TruckCommonDetailActivity.class);
					intent.putExtra("id", info.getObjectId());
					startActivity(intent);
					break;

				default:
					break;
				}
			}
			
		});
		
		mListView.setOnRefreshListener(new OnRefreshListener() {
			
			@Override
			public void onRefresh() {
				pageNo = 1;
				new CommonRequest(mContext, mHandler).getHistoryList(mType, pageNo, PAGE_SIZE);
			}
		});
		mListView.setOnLoadListener(new OnLoadMoreListener() {
			@Override
			public void onLoadMore() {
				new CommonRequest(mContext, mHandler).getHistoryList(mType, pageNo, PAGE_SIZE);
			}
		});
	}
	
	/**
	 * 实现 ActionBar.OnNavigationListener接口
	 */
	class DropDownListenser implements OnNavigationListener {
		// 得到和SpinnerAdapter里一致的字符数组
		String[] listNames = getResources()
				.getStringArray(R.array.order_status);

		/* 当选择下拉菜单项的时候，将Activity中的内容置换为对应的Fragment */
		public boolean onNavigationItemSelected(int itemPosition, long itemId) {
			switch (itemPosition) {
			case 0:
				mType = CommonConfig.OPERATE_ALL;
				break;
			case 1:
				mType = CommonConfig.OPERATE_WATCH;
				break;
			case 2:
				mType=CommonConfig.OPERATE_CALL;
				break;
			case 3:
				mType=CommonConfig.OPERATE_FAVORITE;
				break;
			case 4:
				mType=CommonConfig.OPERATE_GAME;
				break;
			default:
				break;
			}
			onRefesh();
			return true;
		}
	}
	
	public void onRefesh(){
		ProgressDialogHelper.show(mContext, "数据加载中,请稍后...");
		pageNo = 1;
		new CommonRequest(mContext, mHandler).getHistoryList(mType, pageNo, PAGE_SIZE);
	}
	
	@Override
	protected void onDestroy() {
		try {
			mHandler.removeCallbacksAndMessages(null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		super.onDestroy();
	}
}
